// index.js
import api from '../../utils/api'
import {
  isWithin24Hours
} from '../../utils/util'
Page({
  data: {
    sexList: [{
      value: 1,
      name: '男'
    }, {
      value: 2,
      name: '女'
    }],
    disList: [],
    colorList: [],
    carList: [],
    treeData: [],
    dataForm: {
      // operator: '13645557777',
      userCode: '', //,被访人姓名
      inviteCode: '',
      carNo: '',
      carColor: '',
      carColorName: '',
      authType: 2,
      carType: '',
      carTypeName: '',
      carDriverName: '',
      carDriverPhone: '',
      applyEmpName: '',
      applyEmpPhone: '',
      inValidStime: '',
      outValidEtime: '',
      allowImgList: '1#',
      allowOmgList: '1#',
      inFactTask: '',
      remark: '',
      // discharge: '',
      // dischargeName: '',
      inFactPlace: '',
      forwardUnit: '',
      billMdetails: [{
        mbtypeName: '',
        productName: '',
        matSpec: '',
        matNumber: '',
        numberMunit: '',
        matWeight: '',
        weightMunit: '',
      }],
      persons: [],
      file: '',
      receiveUnit: '',
      field4: '',
    },
    isSubmitting: false
  },
  onLoad() {
    const pages = getCurrentPages();
    const currentPage = pages[pages.length - 1];
    const options = currentPage.options; // 获取URL参数
    console.log(options.id);
    api.getDictData({
      dictCode: 'car_color'
    }).then(res => {
      // this.data.dataList = res.data.records
      let arr = []
      res.data.forEach(item => {
        arr.push({
          name: item.dictName,
          value: item.dictValue
        })
      });
      this.setData({
        colorList: arr,
      });
    })
    api.getDictData({
      dictCode: 'TRUCK_TYPE'
    }).then(res => {
      // this.data.dataList = res.data.records
      let arr = []
      res.data.forEach(item => {
        arr.push({
          name: item.dictName,
          value: item.dictValue
        })
      });
      this.setData({
        carList: arr,
      });

    })
    // api.getDictData({
    //   dictCode: 'DISCHARGE'
    // }).then(res => {
    //   // this.data.dataList = res.data.records
    //   let arr = []
    //   res.data.forEach(item => {
    //     arr.push({
    //       name: item.dictName,
    //       value: item.dictValue
    //     })
    //   });
    //   this.setData({
    //     disList: arr,
    //   });

    // })
    api.orgTreeTree({
      orgLevel: '3'
    }).then(res => {

      // this.data.dataList = res.data.records
      let arr = []
      arr = this.flattenTree(res.data)
      arr.forEach(item => {
        item.value = item.id
      });
      console.log(arr, 666)
      this.setData({
        treeData: arr,
      });
    })
  },
  flattenTree(treeData) {
    const result = [];
    const flatten = (node) => {
      result.push(node);
      if (node.childNode) {
        for (const child of node.childNode) {
          flatten(child);
        }
      }
    }
    for (const node of treeData) {
      flatten(node);
    }
    return result;
  },
  bindDateChange1: function (e) {
    this.data.dataForm.inValidStime = e.detail.value
    this.setData({
      dataForm: this.data.dataForm
    })
  },
  bindDateChange2: function (e) {
    this.data.dataForm.outValidEtime = e.detail.value
    this.setData({
      dataForm: this.data.dataForm
    })
  },
  bindPickerChange3(e) {

    this.data.dataForm.receiveUnit = this.data.treeData[e.detail.value].value
    this.data.dataForm.field4 = this.data.treeData[e.detail.value].name
    this.setData({
      dataForm: this.data.dataForm
    })
  },
  bindPickerChange(e) {

    this.data.dataForm.carColor = this.data.colorList[e.detail.value].value
    this.data.dataForm.carColorName = this.data.colorList[e.detail.value].name
    this.setData({
      dataForm: this.data.dataForm
    })
  },
  bindPickerChangeCar(e) {
    console.log(this.data.carList[e.detail.value])
    this.data.dataForm.carType = this.data.carList[e.detail.value].value
    this.data.dataForm.carTypeName = this.data.carList[e.detail.value].name
    this.setData({
      dataForm: this.data.dataForm
    })
  },
  // bindPickerChangedic(e) {
  //   console.log(this.data.disList[e.detail.value])
  //   this.data.dataForm.discharge = this.data.disList[e.detail.value].value
  //   this.data.dataForm.dischargeName = this.data.disList[e.detail.value].name
  //   this.setData({
  //     dataForm: this.data.dataForm
  //   })
  // },
  noInput13: function (e) {
    this.data.dataForm.userCode = e.detail.value
    this.setData({
      dataForm: this.data.dataForm
    })
  },
  noInput: function (e) {
    this.data.dataForm.inviteCode = e.detail.value
    this.setData({
      dataForm: this.data.dataForm
    })
  },
  carNoInput: function (e) {
    this.data.dataForm.carNo = e.detail.value
    this.setData({
      dataForm: this.data.dataForm
    })
  },
  onInput2: function (e) {
    this.data.dataForm.carDriverName = e.detail.value
    this.setData({
      dataForm: this.data.dataForm
    })
  },
  onInput3: function (e) {
    this.data.dataForm.carDriverPhone = e.detail.value
    this.setData({
      dataForm: this.data.dataForm
    })
  },
  onInput4: function (e) {
    this.data.dataForm.applyEmpName = e.detail.value
    this.setData({
      dataForm: this.data.dataForm
    })
  },
  onInput5: function (e) {
    this.data.dataForm.applyEmpPhone = e.detail.value
    this.setData({
      dataForm: this.data.dataForm
    })
  },
  onInput6: function (e) {
    this.data.dataForm.inFactTask = e.detail.value
    this.setData({
      dataForm: this.data.dataForm
    })
  },
  onInput7: function (e) {
    this.data.dataForm.remark = e.detail.value
    this.setData({
      dataForm: this.data.dataForm
    })
  },
  onInput8: function (e) {
    this.data.dataForm.inFactPlace = e.detail.value
    this.setData({
      dataForm: this.data.dataForm
    })
  },
  onInput9: function (e) {
    this.data.dataForm.forwardUnit = e.detail.value
    this.setData({
      dataForm: this.data.dataForm
    })
  },
  getAdd() {
    const {
      carNo,
      carColor,
      carType,
      // discharge,
      carDriverName,
      carDriverPhone,
      applyEmpName,
      applyEmpPhone,
      inValidStime,
      outValidEtime,
      billMdetails,
      receiveUnit,
      userCode,
      inviteCode
    } = this.data.dataForm
    if (!inviteCode.trim()) {
      wx.showToast({
        title: '邀请码不能为空',
        icon: 'none',
      });
      return;
    }
    if (!userCode.trim()) {
      wx.showToast({
        title: '被访人姓名不能为空',
        icon: 'none',
      });
      return;
    }
    if (!carNo.trim()) {
      wx.showToast({
        title: '车牌号不能为空',
        icon: 'none',
      });
      return;
    }
    if (!carColor.trim()) {
      wx.showToast({
        title: '车牌颜色不能为空',
        icon: 'none',
      });
      return;
    }
    if (!carType.trim()) {
      wx.showToast({
        title: '车辆类型不能为空',
        icon: 'none',
      });
      return;
    }
    // if (!discharge.trim()) {
    //   wx.showToast({
    //     title: '排放标准不能为空',
    //     icon: 'none',
    //   });
    //   return;
    // }
    if (!carDriverName.trim()) {
      wx.showToast({
        title: '司机姓名不能为空',
        icon: 'none',
      });
      return;
    }
    if (!carDriverPhone.trim()) {
      wx.showToast({
        title: ' 司机手机号不能为空',
        icon: 'none',
      });
      return;
    }
    if (!applyEmpName.trim()) {
      wx.showToast({
        title: '申请人姓名不能为空',
        icon: 'none',
      });
      return;
    }
    if (!applyEmpPhone.trim()) {
      wx.showToast({
        title: '联系方式不能为空',
        icon: 'none',
      });
      return;
    }
    if (!inValidStime.trim()) {
      wx.showToast({
        title: '有效期始不能为空',
        icon: 'none',
      });
      return;
    }
    if (!outValidEtime.trim()) {
      wx.showToast({
        title: '有效期止不能为空',
        icon: 'none',
      });
      return;
    }
    let time = isWithin24Hours(inValidStime, outValidEtime)
    if (!time) {
      wx.showToast({
        title: '日期区间不能超过24小时',
        icon: 'none',
      });
      return;
    }
    if (!receiveUnit.trim()) {
      wx.showToast({
        title: '到访部室不能为空',
        icon: 'none',
      });
      return;
    }
    if (billMdetails.length == 0) {
      wx.showToast({
        title: '物资清单不能为空',
        icon: 'none',
      });
      return;
    } else {
      let num = 0
      billMdetails.forEach(item => {
        if (!item.matNumber && !item.numberMunit && !item.matWeight && !item.weightMunit) {
          num++
        }
      });
      if (num > 0) {
        wx.showToast({
          title: '物资清单数量、数量单位跟重量、重量单位必须选填一项',
          icon: 'none',
        });
        return;
      }
    }
    if (this.data.isSubmitting) return; // 如果正在提交，则不再处理新的提交请求
    // 设置为正在提交状态
    this.setData({
      isSubmitting: true
    });
    wx.showLoading({
      title: '提交中...',
    });
    api.carOrderAdd(this.data.dataForm).then(res => {
      setTimeout(function () {
        wx.hideLoading();
        wx.redirectTo({
          url: '/pages/truckList/truck', //附带两个参数过去
        })
        // this.setData({
        //   isSubmitting: false
        // }); // 请求成功后重置提交状态

      }, 3000)
    }).catch(e => {
      this.setData({
        isSubmitting: false
      }); // 请求失败后重置提交状态
    })
  },
  showList() {
    wx.redirectTo({
      url: '/pages/truckList/truck', //附带两个参数过去
    })
  },
  personsAdd() {
    this.data.dataForm.persons.push({
      personName: '',
      idCard: '',
      // sexC: '',
      telNum: '',
    })
    this.setData({
      dataForm: this.data.dataForm
    })
  },
  personsDet: function (e) {
    const index = e.currentTarget.dataset.index;
    this.data.dataForm.persons.splice(index, 1);
    this.setData({
      dataForm: this.data.dataForm
    })

  },
  bindPickerChangeSex: function (e) {
    const index = e.currentTarget.dataset.index;
    const name = e.currentTarget.dataset.name;

    if (name == 'personName') {
      this.data.dataForm.persons[index].personName = e.detail.value;
    }
    if (name == 'idCard') {
      this.data.dataForm.persons[index].idCard = e.detail.value;
    }
    if (name == 'telNum') {
      this.data.dataForm.persons[index].telNum = e.detail.value;
    }
    // if (name == 'sexC') {
    //   this.data.dataForm.persons[index].sexC = this.data.sexList[e.detail.value].value;
    //   this.data.dataForm.persons[index].sexCName = this.data.sexList[e.detail.value].name;
    // }
    this.setData({
      dataForm: this.data.dataForm
    })

  },
  personsAdd2() {
    this.data.dataForm.billMdetails.push({
      mbtypeName: '',
      productName: '',
      matSpec: '',
      matNumber: '',
      numberMunit: '',
      matWeight: '',
      weightMunit: '',
    })
    this.setData({
      dataForm: this.data.dataForm
    })
  },
  personsDet2: function (e) {
    const index = e.currentTarget.dataset.index;
    this.data.dataForm.billMdetails.splice(index, 1);
    this.setData({
      dataForm: this.data.dataForm
    })

  },
  bindPickerChangeSex2: function (e) {
    const index = e.currentTarget.dataset.index;
    const name = e.currentTarget.dataset.name;

    if (name == 'mbtypeName') {
      this.data.dataForm.billMdetails[index].mbtypeName = e.detail.value;
    }
    if (name == 'productName') {
      this.data.dataForm.billMdetails[index].productName = e.detail.value;
    }
    if (name == 'matSpec') {
      this.data.dataForm.billMdetails[index].matSpec = e.detail.value;
    }
    if (name == 'matNumber') {
      this.data.dataForm.billMdetails[index].matNumber = e.detail.value;
    }
    if (name == 'numberMunit') {
      this.data.dataForm.billMdetails[index].numberMunit = e.detail.value;
    }
    if (name == 'matWeight') {
      this.data.dataForm.billMdetails[index].matWeight = e.detail.value;
    }
    if (name == 'weightMunit') {
      this.data.dataForm.billMdetails[index].weightMunit = e.detail.value;
    }
    this.setData({
      dataForm: this.data.dataForm
    })

  },
})